﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace EngineOfEvermore.Editor.SceneEditor
{
    public class MainFormController
    {
        readonly App _app;

        public MainFormController( App app )
        {
            _app = app;
        }

        internal void NewDocumentClicked()
        {
            _app.DocumentManager.CreateNewDocument();
        }

        internal void OpenDocumentClicked()
        {
            String nameOfSelectedResource;
            var dialogResult = _app.View.CreateOpenDocumentDialog( out nameOfSelectedResource );

            if ( dialogResult == System.Windows.Forms.DialogResult.OK )
            {
                // Load the scene
                _app.DocumentManager.LoadDocument( nameOfSelectedResource );
            }
        }

        internal void SaveDocumentClicked()
        {
            View.IDocumentView docView = _app.View.MainForm.ActiveDocumentView;

            if ( docView != null )
            {
                SceneModel activeModel = docView.Model;
                Debug.Assert( activeModel != null );
                
                if ( String.IsNullOrEmpty( activeModel.NameOfAssociatedResource ) )
                {
                    String resourceName;
                    if ( _app.View.CreateSaveDocumentDialog( out resourceName ) == DialogResult.OK )
                    {
                        activeModel.SaveSceneAsResource( resourceName );
                    }
                }
                else
                {
                    activeModel.SaveSceneAsResource( activeModel.NameOfAssociatedResource );
                }
            }
        }

        internal void CloseDocumentClicked()
        {
            if ( _app.DocumentManager.ActiveDocument != null )
            {
                var document = _app.DocumentManager.ActiveDocument;
            
                if ( document.Model.HasUnsavedChanges )
                {
                    var result = _app.View.ShowYesNoDialog( "This document contains unsaved changes." 
                        + " Do you want to save the document before closing", "Save Changes?" );
                        
                    if ( result == DialogResult.Yes )
                    {
                        String resourceName;
                        bool abortSaving = false;
                    
                        if ( !String.IsNullOrEmpty( document.Model.NameOfAssociatedResource ) )
                        {
                            resourceName = document.Model.NameOfAssociatedResource;
                        }
                        else
                        {
                           if ( _app.View.CreateSaveDocumentDialog( out resourceName ) != DialogResult.OK )
                           {
                               abortSaving = true;
                           }
                        }
                        
                        if ( !abortSaving )
                        {
                            document.Model.SaveSceneAsResource( resourceName );
                        }
                    }
                }
            
                _app.DocumentManager.CloseDocument( _app.DocumentManager.ActiveDocument );
            }
        }

        internal void ActiveDocumentViewChanged( View.IDocumentView documentView )
        {
            if ( documentView != null )
            {
                var document =
                    _app.DocumentManager.OpenDocuments.Single( doc => doc.View == documentView );

                _app.DocumentManager.ActiveDocument = document;
            }
            else
            {
                _app.DocumentManager.ActiveDocument = null;
            }
        }

        internal void TryClosing( FormClosingEventArgs e )
        {
            if ( _app.DocumentManager.OpenDocuments.Any( doc => doc.Model.HasUnsavedChanges ) )
            {
                var result = _app.View.ShowYesNoDialog( "There are unsaved changes. " + 
                    "Do you really want to exit?", "Unsaved Changes" );

                if ( result == DialogResult.No )
                {
                    e.Cancel = true;
                }
            }
        }
    }
}
